package com.ldk.framework.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import com.ldk.base.framework.dao.impl.BaseDAO;
import com.ldk.base.framework.util.BeanUtil;
import com.ldk.framework.dao.IUserDAO;
import com.ldk.framework.entity.LinkUser;
import com.ldk.framework.entity.User;

/**
* UserDAO接口实现类
* @author xiangwl
*/
@Repository
public class UserDAO extends BaseDAO<User> implements IUserDAO {

	@Override
	public List<User> searchByDistance(String dbKey, User query, double lon,
			double lat, double distance) {
		Assert.notNull(distance);
		Assert.notNull(lon);
		Assert.notNull(lat);
		try{
			Map<String, Object> parms = BeanUtil.toMap(query);
			parms.put("distance", distance);
			parms.put("lon", lon);
			parms.put("lat", lat);
			
			SqlSession sqlSession = getSqlSession(dbKey);
			
			return sqlSession.selectList(getSqlName("searchByDistance"), parms);
		}
		catch(Exception e){
			logger.error(e.getMessage());
		}
		return null;
	}

	@Override
	public LinkUser getLinkUser(String dbKey, String openId) {
		Assert.notNull(openId);
		try{
			LinkUser linkUser = null;
			
			Map<String, Object> parms = new HashMap<String, Object>();
			parms.put("openId", openId);
			
			SqlSession sqlSession = getSqlSession(dbKey);
			
			linkUser = sqlSession.selectOne(getSqlName("getLinkUser"), parms);
			
			return linkUser;
		}
		catch(Exception e){
			logger.error(e.getMessage());
		}
		return null;
	}

}
